home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10155 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  52 lines

  1. Path: news.delcoelect.com!usenet
  2. From: c23kab@kocrsv08.delcoelect.com (Kirk Bailey)
  3. Newsgroups: comp.lang.c
  4. Subject: Evaluation Requirement Question
  5. Date: 15 Mar 1996 20:41:22 GMT
  6. Organization: Delco Electronics Corp.
  7. Distribution: world
  8. Message-ID: <C23KAB.96Mar15154122@kocrsw25.delcoelect.com>
  9. NNTP-Posting-Host: kocrsw25.delcoelect.com
  10.  
  11.    In the following example program, notice that the
  12. test for "i" appears both before and after the call to "foo".
  13. Note that the printf will only be executed (in this example)
  14. due to the last "i != 0" check (i is initialized to 0, and
  15. foo will return 1).  My question:  Is the compiler _required_
  16. to evaluate "i" twice in all cases where it can not determine
  17. that the call to "foo" did not alter "i"?  Are there cases
  18. where "i" doesn't have to be evaluated twice?  Consider some
  19. other possible programs:
  20.  
  21.    1) foo did not _change_ i, or
  22.    2) foo did not _change_ i AND foo's definition occurred
  23.          before its invocation (allowing the compiler to "know"
  24.          this at the point of invocation), or
  25.    3) "i" was a local variable to main (explicitly init. to
  26.          zero) - meaning that no function outside of main could
  27.          alter it through the call to foo.
  28.  
  29.  
  30. #include <stdio.h>
  31.  
  32. int i;
  33. int foo(void);
  34.  
  35. int main(void)
  36. {
  37.   if ( i != 0 || !foo() || i != 0 )
  38.     printf( "got here!\n" );
  39.   return 0;
  40. }
  41.  
  42. int foo(void)
  43. {
  44.   i++;
  45.   return i;
  46. }
  47.  
  48. -- 
  49. Kirk Bailey        (317) 451-0807
  50. Software Engineer, Simulation Tools
  51. Delco Electronics Corporation
  52.